/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.com.poligran.polla.mundialista.ingesoft.impl;


import co.com.poligran.polla.mundialista.ingesoft.base.Partido;
import co.com.poligran.polla.mundialista.ingesoft.dao.PartidoDAO;
import co.com.poligran.polla.mundialista.ingesoft.db.Entitie;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alejo
 */
public class PartidoImpl extends Entitie implements PartidoDAO {
 /* public static void main(String[] args) {
        PartidoDAO dAO = new PartidoImpl();
        Partido u = new Partido(2,new Date(System.currentTimeMillis()),3,2);
       System.out.println(dAO.insert(u));
        for (Partido un : dAO.selectAll()) {
         System.out.println(un);
        } 
      System.out.println("Existe: " + dAO.select(2))
        System.out.println("Delete: " + dAO.delete(3));;
         System.out.println("Update: " + dAO.update(u));

   }*/ 
    
    
    
    @Override
    public boolean insert(Partido e) {
        boolean inserto = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_InsertPartido ?,?,?");

            this.cs.setDate(1, e.getFechaPartido());
            this.cs.setInt(2, e.getIdEquipoA());
            this.cs.setInt(3, e.getIdEquipoB());
           
     

            this.cs.executeUpdate();

            inserto = true;

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return inserto; 
    }

    @Override
    public Partido select(int idEntitie) {
        Partido selectPartido = null;
        try {
            this.query = "EXEC sp_GetPartidoById ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.cs.setInt(1, idEntitie);
            this.rs = cs.executeQuery();
            if (rs.next()) {
                selectPartido = new Partido();
                selectPartido.setFechaPartido(rs.getDate(1));
                selectPartido.setIdEquipoA(rs.getInt(2));
                selectPartido.setIdEquipoB(rs.getInt(3));
            
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());  
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectPartido;
    }

    @Override
    public LinkedList<Partido> selectAll() {
        LinkedList<Partido> selectPartidos = null;
        try {
            this.query = "select * from partido";
            this.ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.rs = ps.executeQuery();
            selectPartidos = new LinkedList();
            while (rs.next()) {
                Partido partido = new Partido();
                partido.setIdPartido(rs.getInt(1));
                partido.setFechaPartido(rs.getDate(2));
                partido.setIdEquipoA(rs.getInt(3));
                partido.setIdEquipoB(rs.getInt(3));
              
                selectPartidos.add(partido);
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectPartidos;
    }

    @Override
    public boolean update(Partido e) {
       boolean update = false;
        try {
            this.cs = conn.prepareCall("sp_UpdatePartido ?,?,?,?");

            this.cs.setInt(1, e.getIdPartido());   
            this.cs.setDate(2, e.getFechaPartido());
            this.cs.setInt(3, e.getIdEquipoA());
            this.cs.setInt(4, e.getIdEquipoB());
          

            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                update = true;
            }

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return update;
    }

    @Override
    public boolean delete(int idEntitie) {
      boolean resp = false;
        try {
            this.query = "EXEC sp_DeletePartido ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // set preparedstatement 
            this.cs.setInt(1, idEntitie);
            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                resp = true;
            }
        } catch (SQLException se) {
            System.out.print("exepction  " + se.getMessage());
        } finally {
            try {
                this.cs.close();
            } catch (SQLException ex) {
                System.out.print("exepction  " + ex.getMessage());
            }
        }
        return resp;
    }
    
    }
    

